#!/usr/bin/env bash

# Copyright 2012-2014 Nigel Small
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


LATEST_1_8_VERSION="1.8.3"
LATEST_1_9_VERSION="1.9.8"
LATEST_2_0_VERSION="2.0.4"
LATEST_2_1_VERSION="2.1.4"
LATEST_VERSION=$LATEST_2_1_VERSION

SELF=$0
COMMAND=$1
shift
ARGS=$@

HOME=`pwd`/`dirname $SELF`
DOWN="$HOME/.bau/down"
LOG="$HOME/.bau/log"
RUN="$HOME/.bau/run"

function install_test_dependencies {
    if [ "$ONLINE" ]
    then
        echo "Ensuring testing dependencies are up to date"
        pip install --upgrade mock pytest pytest-cov
    fi
}

function test {
    echo "Running test suite"
    echo ""
    install_test_dependencies
    pushd "$HOME" > /dev/null
    for NEO4J_VERSION in $NEO4J_VERSIONS
    do
        neo4j_download
        echo "Running tests for Neo4j $NEO4J_VERSION"
        mkdir -p "$LOG/$NEO4J_VERSION" 2> /dev/null
        trap neo4j_stop_and_exit SIGINT SIGTERM
        neo4j_start
        py.test -vx --cov-config .coveragerc --cov py2neo --cov-report term-missing --cov-report html $@
        EXIT_CODE=$?
        trap - SIGINT SIGTERM
        neo4j_stop
        rm -rf "$LOG/$NEO4J_VERSION/htmlcov"
        mv htmlcov "$LOG/$NEO4J_VERSION/htmlcov"
        echo ""
        if [ "$EXIT_CODE" != "0" ]
        then
            echo "Exiting on code $EXIT_CODE after test failure"
            exit $EXIT_CODE
        fi
    done
    popd > /dev/null
    echo ""
    echo "Test suite completed successfully"
    echo ""
}

function download {
    mkdir -p "$DOWN" 2> /dev/null
    pushd "$DOWN" > /dev/null
    wget $@
    popd > /dev/null
}

function neo4j_set_dist {
    EDITION=$NEO4J_EDITION
    if [ "$EDITION" == "" ]
    then
        EDITION="community"
    fi
    VERSION=$NEO4J_VERSION
    if [ "$VERSION" == "2.1" ] || [ "$VERSION" == "" ]
    then
        VERSION=$LATEST_2_1_VERSION
        echo ""
        echo ":::   ::::                  ::     ''     .::::.     .::"
        echo " :::.  ::  :::::   :::::   ::     :::     ''  ::    ''::"
        echo " :: :: :: ::...:: ::   :: ::  ::   ::       .:'       ::"
        echo " ::  '::: ::''''' ::   :: :::::::  ::     .:'         ::"
        echo "::::   ::  :::::   :::::      ::   ::     :::::: :: ::::::"
        echo "                                 :::"
    elif [ "$VERSION" == "2.0" ]
    then
        VERSION=$LATEST_2_0_VERSION
        echo ""
        echo ":::   ::::                  ::     ''     .::::.     ::::"
        echo " :::.  ::  :::::   :::::   ::     :::     ''  ::    ::  ::"
        echo " :: :: :: ::...:: ::   :: ::  ::   ::       .:'     ::  ::"
        echo " ::  '::: ::''''' ::   :: :::::::  ::     .:'       ::  ::"
        echo "::::   ::  :::::   :::::      ::   ::     :::::: ::  ::::"
        echo "                                 :::"
    elif [ "$VERSION" == "1.9" ]
    then
        VERSION=$LATEST_1_9_VERSION
        echo ""
        echo ":::   ::::                  ::     ''      .::       ::::"
        echo " :::.  ::  :::::   :::::   ::     :::     ''::      ::  ::"
        echo " :: :: :: ::...:: ::   :: ::  ::   ::       ::      ':::::"
        echo " ::  '::: ::''''' ::   :: :::::::  ::       ::          ::"
        echo "::::   ::  :::::   :::::      ::   ::     :::::: ::  ::::"
        echo "                                 :::"
    elif [ "$VERSION" == "1.8" ]
    then
        VERSION=$LATEST_1_8_VERSION
        echo ""
        echo ":::   ::::                  ::     ''      .::       ::::"
        echo " :::.  ::  :::::   :::::   ::     :::     ''::      ::  ::"
        echo " :: :: :: ::...:: ::   :: ::  ::   ::       ::       ::::"
        echo " ::  '::: ::''''' ::   :: :::::::  ::       ::      ::  ::"
        echo "::::   ::  :::::   :::::      ::   ::     :::::: ::  ::::"
        echo "                                 :::"
    fi
    DIST="neo4j-$EDITION-$VERSION"
    ARCHIVE="$DIST-unix.tar.gz"
}

function neo4j_download {
    neo4j_set_dist
    if [ ! -f "$DOWN/$ARCHIVE" ]
    then
        if [ "$ONLINE" ]
        then
            download "http://dist.neo4j.org/$ARCHIVE"
        else
            echo "Cannot download necessary files while offline" >&2
            exit 1
        fi
    fi
}

function neo4j_start {
    mkdir -p "$RUN" 2> /dev/null
    pushd "$RUN" > /dev/null
    rm -rf "$RUN/$DIST"
    cp "$DOWN/$ARCHIVE" "$RUN"
    tar zxf "$ARCHIVE"
    rm "$ARCHIVE"
    $DIST/bin/neo4j start
    popd > /dev/null
}

function neo4j_stop {
    pushd "$RUN" > /dev/null
    $DIST/bin/neo4j stop
    popd > /dev/null
}

function neo4j_stop_and_exit {
    neo4j_stop
    exit 130
}

function neo4j {
    SUBCOMMAND=$1
    case $SUBCOMMAND in
        download)
            neo4j_download
            ;;
        start)
            neo4j_start
            ;;
        stop)
            neo4j_stop
            ;;
        *)
            ;;
    esac
}

case $COMMAND in
    download)
        download $ARGS
        ;;
    neo4j)
        neo4j $ARGS
        ;;
    test)
        NEO4J_VERSIONS="1.8 1.9 2.0 2.1"
        test $ARGS
        ;;
    test-latest)
        NEO4J_VERSIONS="2.1"
        test $ARGS
        ;;
esac
